package com.cxkj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxkj.dto.Meentry;
import com.cxkj.dto.Summary;
import com.cxkj.service.SummaryService;

@Controller
@RequestMapping("/summary")
public class SummaryController {
	
	@Autowired
	private SummaryService summaryService;
	
	//转至单据录入页面
	@RequestMapping("/summaryAdd")
	public String summaryAdd(){
		
		return "document-input";
	}
	/**
	 * 新增册号重复检测
	 * @param response
	 * @return
	 */
	@RequestMapping("/sumTest")
	@ResponseBody
	public Map<String,Object> sumTest(Summary summary,HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		List<Meentry> meList = summaryService.sumTest(summary);
		Map<String, Object> result=new HashMap<String, Object>();
		if(meList.size()==0){
				result.put("result", "0");
		}else{
			result.put("result", "1");
		}
		return  result;
	}
	
	/**
	 * 新增录入数据
	 * @return
	 */
	@RequestMapping("/insertSum")
	public String insertSum(Summary summary){
		
		summaryService.insertSum(summary);
		
		return "redirect:/summary/selectSum?pageNow=1";
	}
	
	/**
	 * 单据汇总分页页面
	 * @return
	 */
	@RequestMapping("/selectSum")
	public String selectSum(HttpServletRequest request,Model model){
		
		List<Summary> sumList = summaryService.showSum(request, model);
		
		model.addAttribute("sumList", sumList);
		
		return "input-summary";
	}
	

}
